<html>
  <head>
    <title>倒计时</title>
    <!--以下为css样式-->
    <style type="text/css">
      .daojishi h2 {
        font-family: Helvetica, Microsoft YaHei, Arial, sans-serif;
        font-size: 14px;
        margin-bottom: 5px;
        color: #151515;
      }
      .daojishi #timer {
        font-family: Helvetica, Microsoft YaHei, Arial, sans-serif;
        font-size: 14px;
        color: #151515;
        font-weight: bold;
      }
    </style>
    <script type="text/javascript" src="timer.js"></script>
  </head>
  <body onload="countDown('2021-8-4 9:0:0')">
    <div class="daojishi">
      <h2>剩余时间为：</h2>
      <div id="timer"></div>
    </div>
    <script>
      function countDown(time) {
        var nowTime = +new Date(); //当前时间的毫秒数
        var inputTime = +new Date(time) //用户输入时间的毫秒数
        var times = (inputTime - nowTime) / 1000 //计算剩余的秒数
        var dd = parseInt(times / 60 / 60 / 24); //计算剩余的天数
        dd = dd < 10 ? '0'+ dd : dd;
        var hh = parseInt((times / 60 / 60) % 24); //计算剩余的小时数
        hh = hh < 10 ? '0'+ hh : hh;
        var mm = parseInt(times / 60 % 60); //计算剩余的分钟数
        mm = mm < 10 ? '0'+ mm : mm;
        var ss = parseInt(times % 60); //计算剩余的秒数
        ss = ss < 10 ? '0'+ ss : ss;
        return dd + "天" + hh + "时" + mm + "分" + ss + "秒";
        // var delay1 = setInterval("timer()", 1000);
        // if (dd == 0 && hh == 0 && mm == 0 && ss== 0) {
        //     clearInterval(delay1);
        }
        console.log(countDown('2021-8-4 9:0:0'))
    </script>
  </body>
</html>